package com.bestcem.xm.user.grpc.service.converter;

import com.bestcem.bp.xm.user.grpc.v1.enums.SmtpValidateStatusEnum;
import com.bestcem.bp.xm.user.grpc.v1.services.Smtp;
import com.bestcem.bp.xm.user.grpc.v1.services.SmtpConditon;
import com.bestcem.xm.user.enums.BooleanStatusEnum;
import com.bestcem.xm.user.service.dto.user.UserSmtpDTO;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 邮箱配置grpc对象转换类
 *
 * @author qiongyu.cui <qiongyu.cui@idiaoyan.com>
 * @version v1.0
 * @date 2022/3/17 14:26
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class UserSmtpConverter {

    /**
     * 将邮箱配置对象转成grpc对象输出
     *
     * @param userSmtpDto 邮箱配置信息
     * @return com.bestcem.bp.xm.user.grpc.v1.services.Smtp
     * @author cuiqiongyu <qiongyu.cui@idiaoyan.cn>
     * @date 2022/03/17 14:28
     */
    public static Smtp dtoToSmtp(UserSmtpDTO userSmtpDto) {
        Smtp.Builder builder = Smtp.newBuilder();
        if (Objects.nonNull(userSmtpDto)) {
            if (Objects.nonNull(userSmtpDto.getSmtpId())) {
                builder.setId(userSmtpDto.getSmtpId());
            }
            if (Objects.nonNull(userSmtpDto.getOrgId())) {
                builder.setOrgId(userSmtpDto.getOrgId());
            }
            if (Objects.nonNull(userSmtpDto.getAccount())) {
                builder.setAccount(userSmtpDto.getAccount());
            }
            if (Objects.nonNull(userSmtpDto.getPassword())) {
                builder.setPassword(userSmtpDto.getPassword());
            }
            if (Objects.nonNull(userSmtpDto.getHost())) {
                builder.setHost(userSmtpDto.getHost());
            }
            if (Objects.nonNull(userSmtpDto.getPort())) {
                builder.setPort(userSmtpDto.getPort());
            }
            builder.setStatus(SmtpValidateStatusEnum.SmtpValidateStatus.forNumber(userSmtpDto.getValidateStatus()));
            if (Objects.nonNull(userSmtpDto.getSslFlag())) {
                builder.setSsl(BooleanStatusEnum.toBoolean(userSmtpDto.getSslFlag()));
            }
            if (Objects.nonNull(userSmtpDto.getDeliverFlag())) {
                builder.setDeliver(BooleanStatusEnum.toBoolean(userSmtpDto.getDeliverFlag()));
            }
            if (Objects.nonNull(userSmtpDto.getNoticeFlag())) {
                builder.setNotice(BooleanStatusEnum.toBoolean(userSmtpDto.getNoticeFlag()));
            }
            if (Objects.nonNull(userSmtpDto.getSystemFlag())) {
                builder.setSystem(BooleanStatusEnum.toBoolean(userSmtpDto.getSystemFlag()));
            }
            if (Objects.nonNull(userSmtpDto.getSenderName())) {
                builder.setSenderName(userSmtpDto.getSenderName());
            }
            if (Objects.nonNull(userSmtpDto.getDescription())) {
                builder.setDescription(userSmtpDto.getDescription());
            }
        }
        return builder.build();
    }

    /**
     * 将邮箱配置列表对象转换成grpc对象输出
     * @param userSmtpDtos
     * @return java.util.List<com.bestcem.bp.xm.user.grpc.v1.services.Smtp>
     * @author cuiqiongyu <qiongyu.cui@idiaoyan.cn>
     * @date 2022/03/17 15:21
     */
    public static List<Smtp> dtosToSmtps(List<UserSmtpDTO> userSmtpDtos) {
        List<Smtp> smtps = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(userSmtpDtos)) {
            for (UserSmtpDTO userSmtpDto : userSmtpDtos) {
                smtps.add(dtoToSmtp(userSmtpDto));
            }
        }
        return smtps;
    }

    /**
     * 将grpc对象转换成DTO对象
     *
     * @param smtp grpc邮箱配置对象
     * @return com.bestcem.xm.user.service.dto.user.UserSmtpDTO
     * @author cuiqiongyu <qiongyu.cui@idiaoyan.cn>
     * @date 2022/03/17 14:43
     */
    public static UserSmtpDTO smtpToDto(SmtpConditon smtp) {
        UserSmtpDTO userSmtpDTO = new UserSmtpDTO();
        if (Objects.nonNull(smtp)) {
            userSmtpDTO.setOrgId(smtp.getOrgId());
            if (smtp.hasId()) {
                userSmtpDTO.setSmtpId(smtp.getId());
            }
            if (smtp.hasAccount()) {
                userSmtpDTO.setAccount(smtp.getAccount());
            }
            if (smtp.hasPassword()) {
                userSmtpDTO.setPassword(smtp.getPassword());
            }
            if (smtp.hasHost()) {
                userSmtpDTO.setHost(smtp.getHost());
            }
            if (smtp.hasPort()) {
                userSmtpDTO.setPort(Integer.valueOf(smtp.getPort()));
            }
            if (smtp.hasStatus()) {
                userSmtpDTO.setValidateStatus(smtp.getStatusValue());
            }
            if (smtp.hasSsl()) {
                userSmtpDTO.setSslFlag(BooleanStatusEnum.toInteger(smtp.getSsl()));
            }
            if (smtp.hasDeliver()) {
                userSmtpDTO.setDeliverFlag(BooleanStatusEnum.toInteger(smtp.getDeliver()));
            }
            if (smtp.hasNotice()) {
                userSmtpDTO.setNoticeFlag(BooleanStatusEnum.toInteger(smtp.getNotice()));
            }
            if (smtp.hasSystem()) {
                userSmtpDTO.setSystemFlag(BooleanStatusEnum.toInteger(smtp.getSystem()));
            }
            if (smtp.hasSenderName()) {
                userSmtpDTO.setSenderName(smtp.getSenderName());
            }
            if (smtp.hasDescription()) {
                userSmtpDTO.setDescription(smtp.getDescription());
            }
        }

        return userSmtpDTO;
    }

}
